/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.chart;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import cn.edu.zju.vlis.aus.bean.UsageRecord;
import cn.edu.zju.vlis.aus.db.UsageRecordDAO;
import cn.edu.zju.vlis.aus.util.IconCache;

public class BarChartAdapter extends BaseAdapter {
	private Context context;
	private List<UsageRecord> recList;

	public BarChartAdapter(Context context, UsageRecordDAO dao) {
		this.context = context;
		recList = dao.getRankListByCount();
		if (!recList.isEmpty()) {
			BarView.maxCount = recList.get(0).getUseCount();
			BarView.textBounds = null;
		}
	}

	@Override
	public int getCount() {
		return recList.size();
	}

	@Override
	public Object getItem(int position) {
		return recList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		UsageRecord rec = (UsageRecord) getItem(position);
		if (convertView == null) {
			convertView = new BarChartItemView(context);
		}
		BarChartItemView itemView = (BarChartItemView) convertView;
		itemView.setIcon(IconCache.getIcon(rec.getPackageName()));
		itemView.setAppName(rec.getAppName());
		itemView.setCount(rec.getUseCount());
		itemView.setPagckageName(rec.getPackageName());
		return convertView;
	}

}
